/**
 * 
 */
package cn.panxing.server.search;

import java.io.*;

import org.apache.lucene.queryParser.ParseException;

/**
 * @author allen
 *
 */
public class FileSearch {

	private static final String HOME_ALLEN_INDEX = "/home/allen/index/";

	/**
	 * @param args
	 * @throws ParseException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException, ParseException {
		if ( args.length < 2 ) {
			System.out.println("Usage: java FileSearch {filename | -i indexdir}");
			
			return ;
		}
		
		if ( args.length == 3){
			System.out.println(args[2]);
			doIndex(args[2]);
		}
		
		if ( args.length == 2){
			
			System.out.println(args[1]);
			doSearch( args[1] );
		}
		

	}

	/**
	 * @throws IOException
	 * @throws ParseException 
	 */
	private static void doIndex(String documentDir) throws IOException, ParseException {
		TextFileIndexer fileIndex= new TextFileIndexer(documentDir,HOME_ALLEN_INDEX);
		 fileIndex.doIndex();
	}

	/**
	 * @param args
	 * @throws IOException
	 * @throws ParseException
	 */
	private static void doSearch(String queryword) throws IOException, ParseException {
		TextFileSearcher fileSearch = new TextFileSearcher( HOME_ALLEN_INDEX );
		String results[] = fileSearch.doSearch(queryword);
		
		for(int i=0; i<results.length; ++i){
			System.out.println( results[i]);
		}
	}

}
